package com.ncpowernode.javaweb.service.impl;

import com.ncpowernode.javaweb.bean.Book;
import com.ncpowernode.javaweb.dao.BookMapper;
import com.ncpowernode.javaweb.service.BookService;
import com.ncpowernode.javaweb.utils.SqlSessionUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

public class BookServiceImpl implements BookService {


    private SqlSession sqlsession = SqlSessionUtil.openSession();

    /**
     * 图书展示
     * @param request
     * @param response
     */
    @Override
    public void list(HttpServletRequest request, HttpServletResponse response) {
        BookMapper mapper = sqlsession.getMapper(BookMapper.class);
        List<Book> books = mapper.selectAll();// 获取到图书馆的书籍相关信息
        request.setAttribute("books",books);// 将数据给控制层，由控制层调视图层去渲染
    }

    /**
     * 添加图书
     * @param request
     * @param response
     */
    @Override
    public void add(HttpServletRequest request, HttpServletResponse response) {
        String bookName = request.getParameter("bookName");
        String appraise = request.getParameter("appraise");
        String author = request.getParameter("author");
        Double price = Double.valueOf(request.getParameter("price"));
        Book book = new Book(null, bookName, appraise, author, price);
        BookMapper mapper = sqlsession.getMapper(BookMapper.class);
        int cnt = mapper.insert(book);
        if(cnt==1){
            sqlsession.commit();
        }
    }

    /**
     * 修改图书的评价或者价格
     * @param request
     * @param response
     */
    @Override
    public void modify(HttpServletRequest request, HttpServletResponse response) {
        Long id = Long.valueOf(request.getParameter("id"));
        BookMapper mapper = sqlsession.getMapper(BookMapper.class);
        Book book = mapper.selectOneById(id);
        request.setAttribute("book",book);// 放入域中，请求转发给相应的jsp 渲染
    }

    /**
     * 删除图书
     * @param request
     * @param response
     */
    @Override
    public void delete(HttpServletRequest request, HttpServletResponse response) {
        Long id = Long.valueOf(request.getParameter("id"));
        BookMapper mapper = sqlsession.getMapper(BookMapper.class);
        int cnt = mapper.deleteById(id);
        if(cnt==1){
            sqlsession.commit();
        }
    }

    /**
     * 查看图书的评价
     * @param request
     * @param response
     */
    @Override
    public void appraise(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String appraise = request.getParameter("appraise");
        request.setAttribute("appraise",appraise);
        request.getRequestDispatcher("/appraise.jsp").forward(request,response);
    }

    /**
     * 修改价格和评价后进行保存
     * @param request
     * @param response
     */
    @Override
    public void save(HttpServletRequest request, HttpServletResponse response) {
        // 获取数据
        String bookName = request.getParameter("book_name");
        String appraise = request.getParameter("appraise");
        Double price = Double.valueOf(request.getParameter("price"));
        // 进行保存
        BookMapper mapper = sqlsession.getMapper(BookMapper.class);
        int cnt = mapper.updateByBookName(bookName, appraise, price);
        if (cnt==1) {
            sqlsession.commit();
        }
    }
}
